package com.bRoom.controller.back;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.pojo.Message;
import com.bRoom.pojo.PayInfo;
import com.bRoom.pojo.Room;
import com.bRoom.pojo.User;
import com.bRoom.service.IFileService;
import com.bRoom.service.IRoomService;
import com.bRoom.util.PropertiesUtil;
import com.bRoom.vo.RoomDetailVo;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("back/room")
public class IRoomManageController {

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IFileService iFileService;



    @RequestMapping("add_room.do")
    @ResponseBody
    public ServerResponse<Room> addRoom(Room room, HttpSession session){

        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
             return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

             return roomService.addRoom(room);
        }
         return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");
    }

    @RequestMapping("update_room.do")
    @ResponseBody
    public ServerResponse<Room> updateRoom(Room room,HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

            return roomService.updateRoom(room);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");
    }


    @RequestMapping("stop_room.do")
    @ResponseBody
    public  ServerResponse stopRooom(Integer roomId,HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

            return roomService.stopRoom(roomId);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");
    }

    @RequestMapping("check_out_room.do")
    @ResponseBody
    public  ServerResponse checkOutRoom(Integer roomId,HttpSession session){

        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

            return roomService.checkOutRoom(roomId);
        }
        return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");

    }

    @RequestMapping("roomDetail.do")
    @ResponseBody
    public ServerResponse<RoomDetailVo> roomDetail(Integer roomId, HttpSession session){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_ADMIN || user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

            return roomService.roomDetail(roomId);
        }

        return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");

    }


    @RequestMapping("roomList.do")
    @ResponseBody
    public ServerResponse<PageInfo> roomList (HttpSession session, @RequestParam(value = "pageSize",defaultValue = "1") int pageSize
                                                                , @RequestParam(value = "pageNum",defaultValue = "8") int pageNum){

          User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_ADMIN || user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

            return roomService.roomList(pageSize,pageNum);
        }

        return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");

    }


    @RequestMapping("uploadImg.do")
    @ResponseBody
    public  ServerResponse uploadImg(HttpServletRequest request,@RequestParam(value = "uploadFile",required = false) MultipartFile file){
           String path=request.getSession().getServletContext().getRealPath("upload");

           String targetFile=iFileService.upload(file,path);
           String url= PropertiesUtil.getProperties("ftp.server.http.prefix","http://image.imooc.com:8089/")+targetFile;

        Map resultMap= Maps.newHashMap();
        resultMap.put("uri",targetFile);
        resultMap.put("url",url);
        return ServerResponse.createBySuccessData(resultMap);
    }

    @RequestMapping("searchRoom.do")
    @ResponseBody
    public ServerResponse<PageInfo> searchRoom(String roomName,HttpSession session,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "5") int  pageSize){
        User user=(User) session.getAttribute(Conts.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
        }
        if (user.getUserRole()==Conts.ROLE.ROLE_ADMIN || user.getUserRole()==Conts.ROLE.ROLE_SUPPER){

            return roomService.searchRoom(roomName,pageNum,pageSize);
        }

        return ServerResponse.createByErrorMsg("对不起，你没有该操作权限");

    }
}
